﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using com.lover.astd.common.config;
using com.lover.astd.common.model;
using com.lover.astd.common;

namespace com.lover.astd.game.ui.server.impl.economy
{
    /// <summary>
    /// 贸易服务
    /// </summary>
    class StockServer : LogicServer
    {
        public StockServer(MainForm frm) { _mainForm = frm; ServerName = ConfigStrings.S_Stock; ServerReadableName = ConfigStrings.SR_Stock; }
        public override void renderSettings()
        {
            MainForm frm = _mainForm;
            Dictionary<string, string> conf = getConfig(ServerName);
            if (conf.Count == 0) return;
            if (conf.ContainsKey(ConfigStrings.enabled))
            {
                frm.chk_stock_enable.Checked = conf[ConfigStrings.enabled].ToLower().Equals("true");
            }
            if(conf.ContainsKey(ConfigStrings.stock_id))
            {
                frm.chk_stock_1.Checked = false;
                frm.chk_stock_2.Checked = false;
                frm.chk_stock_3.Checked = false;
                frm.chk_stock_4.Checked = false;
                frm.chk_stock_5.Checked = false;
                frm.chk_stock_6.Checked = false;
                frm.chk_stock_7.Checked = false;
                string stock_ids = conf[ConfigStrings.stock_id];
                string[] stock_idstrs = stock_ids.Split(',');
                foreach (string stock_idstr in stock_idstrs)
                {
                    if (stock_idstr.Length == 0) continue;
                    int stockid = 0;
                    Int32.TryParse(stock_idstr, out stockid);
                    if (stockid < 1 || stockid > 7) continue;
                    if (stockid == 1) frm.chk_stock_1.Checked = true;
                    if (stockid == 2) frm.chk_stock_2.Checked = true;
                    if (stockid == 3) frm.chk_stock_3.Checked = true;
                    if (stockid == 4) frm.chk_stock_4.Checked = true;
                    if (stockid == 5) frm.chk_stock_5.Checked = true;
                    if (stockid == 6) frm.chk_stock_6.Checked = true;
                    if (stockid == 7) frm.chk_stock_7.Checked = true;
                }
            }
        }
        public override void saveSettings()
        {
            MainForm frm = _mainForm;
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, frm.chk_stock_enable.Checked.ToString());
            string stock_ids = "";
            if (frm.chk_stock_1.Checked) stock_ids += "1,";
            if (frm.chk_stock_2.Checked) stock_ids += "2,";
            if (frm.chk_stock_3.Checked) stock_ids += "3,";
            if (frm.chk_stock_4.Checked) stock_ids += "4,";
            if (frm.chk_stock_5.Checked) stock_ids += "5,";
            if (frm.chk_stock_6.Checked) stock_ids += "6,";
            if (frm.chk_stock_7.Checked) stock_ids += "7,";

            if (stock_ids.Length > 0 && stock_ids[stock_ids.Length - 1].Equals(",")) 
                stock_ids = stock_ids.Substring(0, stock_ids.Length - 1);

            conf.setConfig(ServerName, ConfigStrings.stock_id, stock_ids); // 苹果/木材
        }
        public override void loadDefaultSettings()
        {
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, "true");
            conf.setConfig(ServerName, ConfigStrings.stock_id, "2,3"); // 苹果/木材
            renderSettings();
        }
        
    }
}
